OPTIONAL Stacks.f

Requires Dorothy.f
Only Forth Definitions

type: stack
   int.pointer top
end-type

: stack,   here ,  cells allot ;

: stack: ( n -- <name> )   here  swap  stack,  stack.const ;

\ : push ( x stack -- )   cell over +! @ ! ;
icode stack.push
   0 [ebx] ecx mov  \ get pointer
   0 [ebp] eax mov  \ value
   cell # 0 [ebx] add  \ inc pointer
   eax 4 [ecx] mov  \ store value
   4 [EBP] EBX MOV  \ 2drop
   8 # EBP ADD
   ret end-code

: stack.clear  ( stack -- )   dup ! ;

: stack.empty?   dup @ = ;

\ : discard   -cell swap +! ;

icode stack.discard
   4 # 0 [ebx] sub
   pop(ebx)
   ret end-code

: stack.pop ( stack -- x )
   [defined] check-stacks [if]  dup empty? abort" error: stack empty"  [then]
   dup  top   @   swap  discard ;

\ For using stacks to make lists
: stack.count  ( stack -- addr count )
   @+ over - cell / 1 + ;
